<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../bower_components/polymer/polymer.html">

<link rel="import" href="../bower_components/app-layout/app-drawer/app-drawer.html">
<link rel="import" href="../bower_components/app-layout/app-drawer-layout/app-drawer-layout.html">
<link rel="import" href="../bower_components/app-layout/app-header/app-header.html">
<link rel="import" href="../bower_components/app-layout/app-header-layout/app-header-layout.html">
<link rel="import" href="../bower_components/app-layout/app-scroll-effects/app-scroll-effects.html">
<link rel="import" href="../bower_components/app-layout/app-toolbar/app-toolbar.html">
<link rel="import" href="../bower_components/app-route/app-location.html">
<link rel="import" href="../bower_components/app-route/app-route.html">

<link rel="import" href="../bower_components/iron-pages/iron-pages.html">
<link rel="import" href="../bower_components/iron-selector/iron-selector.html">

<link rel="import" href="../bower_components/paper-icon-button/paper-icon-button.html">

<link rel="import" href="../bower_components/uniflow-polymer/action-emitter.html">
<link rel="import" href="../bower_components/uniflow-polymer/application-state.html">

<link rel="import" href="inmoodforlife-action-dispatcher.html">
<link rel="import" href="inmoodforlife-icons.html">

<dom-module id="inmoodforlife-app">
  <template>
    <style>
      :host {
        --app-primary-color: #4285f4;
        --app-secondary-color: black;

        display: block;
      }

      app-header {
        color: #fff;
        background-color: var(--app-primary-color);
      }
      app-header paper-icon-button {
        --paper-icon-button-ink-color: white;
      }

      .drawer-list {
        margin: 0 20px;
      }

      .drawer-list div {
        display: block;
        cursor: pointer;
        padding: 0 16px;
        text-decoration: none;
        color: var(--app-secondary-color);
        line-height: 40px;
      }

      .drawer-list div.iron-selected {
        color: black;
        font-weight: bold;
      }

      #pageSelector {
        height: 100%;
      }

    </style>


    <inmoodforlife-action-dispatcher state="{{state}}"></inmoodforlife-action-dispatcher>

    <app-location 
        route="{{route}}"        
        use-hash-as-path></app-location>
    <app-route
        route="{{route}}"
        pattern="/:page"
        data="{{routeData}}"
        tail="{{subroute}}"></app-route>

    <app-drawer-layout  fullbleed  force-narrow>
      <!-- Drawer content -->
      <app-drawer id="drawer">
        <app-toolbar>Menu</app-toolbar>
        <iron-selector selected="[[page]]" attr-for-selected="name" class="drawer-list" role="navigation">
          <div name="init" on-tap="_changePage">Init</div>
          <div name="paired" on-tap="_changePage"></div>
          <div name="scanning" on-tap="_changePage"></div>
        </iron-selector>
      </app-drawer>

      <!-- Main content -->
      <app-header-layout has-scrolling-region>

        <app-header condenses reveals effects="waterfall">
          <app-toolbar>
            <paper-icon-button icon="inmoodforlife:menu" drawer-toggle></paper-icon-button>
            <div main-title>InMoodForLife</div>
          </app-toolbar>
        </app-header>

        <iron-pages
            id="pageSelector"
            selected="[[page]]"
            attr-for-selected="name"
            fallback-selection="view404"
            role="main">
          <inmoodforlife-init
               name="init"></inmoodforlife-init>
          <inmoodforlife-scanning 
              name="scanning"
              selected="[[page]]"></inmoodforlife-scanning>
          <inmoodforlife-paired
              name="paired"
              selected="[[page]]"></inmoodforlife-paired>
          <inmoodforlife-view404 
              name="view404"></inmoodforlife-view404>
        </iron-pages>
      </app-header-layout>
    </app-drawer-layout>
  </template>

  <script>
    Polymer({
      is: 'inmoodforlife-app',

      behaviors: [
        UniFlow.ActionEmitter,
        UniFlow.ApplicationState,
      ],

      properties: {
        page: {
          type: String,
          reflectToAttribute: true,
          observer: '_pageChanged',
        },
      },

      observers: [
        '_routePageChanged(routeData.page)',
      ],


      // ***********************************************************************
      // Livecycle
      // ***********************************************************************

      ready: function() {
        this.set('state', {  });
      },

      // ***********************************************************************
      // Observers
      // ***********************************************************************



      _routePageChanged: function(page) {
        console.debug("[inmoodforlife-app] _routePageChanged", page);
        this.page = page || 'init';

        if (!this.$.drawer.persistent) {
          this.$.drawer.close();
        }
      },

      _pageChanged: function(page) {
        console.debug("[inmoodforlife-app] _pageChanged", page);
        // Load page import on demand. Show 404 page if fails
        var resolvedPageUrl = this.resolveUrl('inmoodforlife-' + page + '.html');
        this.importHref(resolvedPageUrl, null, this._showPage404, true);
      },

      _showPage404: function() {
        this.page = 'view404';
      },

      _changePage: function(evt) {
        console.debug("[inmoodforlife-app] _changePage", evt.target.getAttribute("name"));
        // Emits a CHANGE_PAGE action
        this.emitAction({
          type: inmoodforlifeApp.actions.CHANGE_PAGE,
          pageId: evt.target.getAttribute("name")
        });        
      },
    });
  </script>
</dom-module>
